package com.huaxin.hxcloud.activity;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.huaxin.common.BaseActivity;
import com.huaxin.common.Constants;
import com.huaxin.common.Entity.BaseDTO;
import com.huaxin.common.Entity.DataValuesBean;
import com.huaxin.common.Entity.DeviceDataListDTO;
import com.huaxin.common.Entity.DeviceListDTO;
import com.huaxin.common.Entity.HxChangeValveBean;
import com.huaxin.common.Entity.HxSPTTargetDTO;
import com.huaxin.common.Entity.HxScStationRealDTO;
import com.huaxin.common.tools.HxTransmitRequestHelper;
import com.huaxin.common.tools.Log;
import com.huaxin.common.tts.Constant;
import com.huaxin.common.utils.DialogUtil;
import com.huaxin.common.utils.HxUtils;
import com.huaxin.common.utils.ToastUtil;
import com.huaxin.common.view.BottomSheetDialog;
import com.huaxin.hxcloud.R;
import com.huaxin.hxcloud.dialog.HxSPTControlTargetDialog;
import com.huaxin.hxcloud.dialog.HxSPTNotInitTargetDialog;
import com.huaxin.hxcloud.dialog.HxSPTSimulateTargerDialog;
import com.huaxin.hxcloud.dialog.HxSetFacilityControlDialog;
import com.huaxin.hxcloud.dialog.MessageCodeCheckDialog;
import com.huaxin.hxcloud.dialog.SptRemoteContralDialog;
import com.huaxin.hxcloud.work.HxSPTInitTargetFragment;
import com.huaxin.hxcloud.work.HxStationRealDataAdapter;
import com.huaxindata.common.util.HxLogUtils;
import com.huaxindata.im.main.manager.UserManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/9/28.
 * 首创指标
 */
public class StationRealDataActivity extends BaseActivity {
    private static final String TAG = "StationRealDataActivity";
    @BindView(R.id.more)
    TextView       mMore;
    @BindView(R.id.toolbar)
    Toolbar        mToolbar;
    @BindView(R.id.img)
    ImageView      mImg;
    @BindView(R.id.owner)
    TextView       mOwner;
    @BindView(R.id.date)
    TextView       mDate;
    @BindView(R.id.progressbar)
    ProgressBar    mProgressbar;
    @BindView(R.id.tv_device_box)
    TextView       mTvDeviceBox;
    @BindView(R.id.content_lv)
    RelativeLayout mContentLv;
    @BindView(R.id.recyclerview)
    RecyclerView   mRecyclerview;
    @BindView(R.id.content)
    FrameLayout    mContent;


    private String deviceCode;
    private String mStationCode;
    private boolean isTimer = false;//是否定时刷新
    private Timer                    mMTimer;
    private TimerTask                mTimerTask;
    private HxStationRealDataAdapter mRealDataAdapter;
    private int count = 5;
    private HxSetFacilityControlDialog         mControlDialog;
    private DeviceListAdapter                  deviceListAdapter;
    private List<DeviceListDTO.DeviceDateBean> deviceList;
    private BottomSheetDialog                  mDialog;
    private MessageCodeCheckDialog             messageCodeCheckDialog;
    private ArrayList<DataValuesBean>          dataValueslist;
    private int                                isSmalldevise;
    private HxSPTInitTargetFragment            mTargetFragment;
    private HxSPTControlTargetDialog           mControlTargetDialog;
    private boolean isInitTarget = false;
    private SptRemoteContralDialog sptRemoteContralDialog;
    private int deviceStata = -4; //保存设备状态

    @Override
    protected int getContentViewId() {
        return R.layout.activity_station_realdata;
    }

    @Override
    protected int getLoadFrameId() {
        return R.id.content;
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        if (!extras.containsKey(Constants.IK_DEVICE_CODE)) {
            finish();
        }
        deviceCode = extras.getString(Constants.IK_DEVICE_CODE);

        dataValueslist = extras.getParcelableArrayList(Constants.IK_TARGET_DATA);
        isSmalldevise = extras.getInt("smalldeviecdata");
        mStationCode = extras.getString("STATION_CODE");
    }

    @Override
    public void initView() {
        mToolbar.setTitle("实时状态");
        mMore.setText("初始化");
        mTvDeviceBox.setVisibility(View.INVISIBLE);
        setSupportActionBar(mToolbar);
        initRecyclerView();
        if (isSmalldevise == 1) {
            //查看自设备指标
            if (dataValueslist == null || dataValueslist.size() <= 0) {
                ToastUtil.showToast("暂无指标");
            } else {
                mRealDataAdapter.setNewData(dataValueslist);
            }
            updateLoadStatus(LOAD_SUCCESS);
        } else {
            onTimer(mDate, mProgressbar);
        }


        if (!Constant.SPTOrgcodeFirst.equals(UserManager.getFirstOrgCode())) {
            mMore.setVisibility(View.GONE);
        } else {
            mMore.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void requestData() {
        if (isSmalldevise == 1) {
            return;
        }
        deviceListAdapter = new DeviceListAdapter();
        ApiRequestData(LOADING, isTimer, null);
        //设备柜
        ApiRequestSubDevice();

        //spt 获取设备状态
        if (Constant.SPTOrgcodeFirst.equals(UserManager.getFirstOrgCode())) {
            getTargetStatus(false);
        }

    }

    /**
     * 新增接口，获取设备集合
     */
    private void ApiRequestSubDevice() {
        Call<DeviceListDTO> call = HxTransmitRequestHelper.getRequestApi().getSubDevice(deviceCode);
        autoClose(call);
        call.enqueue(new Callback<DeviceListDTO>() {
            @Override
            public void onResponse(Call<DeviceListDTO> call, Response<DeviceListDTO> response) {
                DeviceListDTO deviceDataDTO = response.body();
                if (response.isSuccessful() && deviceDataDTO != null) {
                    //刷新底部弹框设备分栏
                    deviceList = deviceDataDTO.getDeviceList();
                    if (deviceList != null && deviceList.size() > 0) {
                        deviceListAdapter.setNewData(deviceList);
                        mTvDeviceBox.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            public void onFailure(Call<DeviceListDTO> call, Throwable t) {
                Log.d(TAG, "onResponse: " + t);
            }
        });
    }

    /**
     * 获取设备指标
     *
     * @param status
     * @param timer
     * @param progressBar
     */
    private void ApiRequestData(int status, boolean timer, final ProgressBar progressBar) {
        if (!timer) {
            updateLoadStatus(status);
        }
        Call<DeviceDataListDTO> call = HxTransmitRequestHelper.getRequestApi().getDeviceData(mStationCode, deviceCode);
        autoClose(call);
        call.enqueue(new Callback<DeviceDataListDTO>() {
            @Override
            public void onResponse(Call<DeviceDataListDTO> call, Response<DeviceDataListDTO> response) {
                DeviceDataListDTO deviceDataDTO = response.body();
                if (deviceDataDTO != null && deviceDataDTO.isSuccessful()) {
                    for (DeviceDataListDTO.DeviceDataListBean deviceDataListBean : deviceDataDTO.getDeviceDataList()) {
                        if (deviceDataListBean.getDataValues().size() == 0) {
                            ToastUtil.showToast("暂无相关指标！");
                            releaseTimer();
                        } else {
                            mRealDataAdapter.setNewData(deviceDataListBean.getDataValues());
                        }
                    }
                    updateLoadStatus(LOAD_SUCCESS);
                } else {
                    ToastUtil.showToast("暂无相关指标！");
                    releaseTimer();
                    updateLoadStatus(LOAD_ERROR);
                }
                if (progressBar != null) {
                    progressBar.setVisibility(View.GONE);
                }
            }

            @Override
            public void onFailure(Call<DeviceDataListDTO> call, Throwable t) {
                updateLoadStatus(LOAD_FAILURE);
                if (progressBar != null) {
                    progressBar.setVisibility(View.GONE);
                }
                releaseTimer();
            }
        });
    }

    /**
     * 定时器
     *
     * @param dateTv
     * @param progressBar
     */
    private void onTimer(final TextView dateTv, final ProgressBar progressBar) {
        progressBar.setVisibility(View.GONE);
        mMTimer = new Timer();
        @SuppressLint("HandlerLeak") final Handler doActionHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case 1:
                        count--;
                        if (count == 0) {
                            progressBar.setVisibility(View.VISIBLE);
                            ApiRequestData(LOADING, !isTimer, progressBar);
                            count = 5;
                        }
                        dateTv.setText((count + "s") + "后更新数据");
                        break;
                }
            }
        };
        mTimerTask = new TimerTask() {
            @Override
            public void run() {
                Message message = new Message();
                message.what = 1;
                doActionHandler.sendMessage(message);
            }
        };
        mMTimer.schedule(mTimerTask, 1000, 1000);
    }

    private void initRecyclerView() {
        mRealDataAdapter = new HxStationRealDataAdapter(this);
        mRecyclerview.setLayoutManager(new GridLayoutManager(this, 2));
        mRecyclerview.setAdapter(mRealDataAdapter);

        //指标列表item 点击
        mRealDataAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                final DataValuesBean valuesBean = mRealDataAdapter.getData().get(position);
                final String[] array;
                if (!("rw").equals(valuesBean.getRw())) {
                    //develop2分支中不考虑首创业务逻辑
                    if (HxUtils.isScOrg(UserManager.getFirstOrgCode())) {
                        array = new String[]{"查看历史数据", "查看近日曲线"};
                    } else {
                        array = new String[]{"查看历史数据"};
                    }
                } else {
                    array = new String[]{"查看历史数据", "远控相关指标"};
                }
                //spt以外的远控 全部采取固定密码
                final BottomSheetDialog dialog = new BottomSheetDialog(StationRealDataActivity.this, array, new
                        BottomSheetDialog.OnItemClickListener() {
                            @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
                            @Override
                            public void onItemClick(int position) {
                                if (("rw").equals(valuesBean.getRw())) {
                                    //可远控设备
                                    if (position == 0) {
                                        // 跳转到历史数据
                                        Intent intent = new Intent(StationRealDataActivity.this, ChartActivity.class);
                                        intent.putExtra("fullCode", valuesBean.getFullCode());
                                        startActivity(intent);
                                    } else if (position == 1) {
                                        //SPT 直接远控指标，其他平台采用固定密码设置
                                        if (Constant.SPTOrgcodeFirst.equals(UserManager.getFirstOrgCode())) {
                                            if (isInitTarget) {
                                                sptShowControlTargetDialog(valuesBean.getFullCode(), valuesBean.getName(), valuesBean.getDataMold(), valuesBean.getValue());
                                            } else {
                                                HxSPTNotInitTargetDialog notInitTargetDialog = HxSPTNotInitTargetDialog.newInstance();
                                                notInitTargetDialog.show(getFragmentManager(), "notinit");
                                            }
                                            return;
                                        }
                                        mControlDialog = new HxSetFacilityControlDialog(StationRealDataActivity.this, valuesBean.getName(), new HxSetFacilityControlDialog.OnItemClickListener() {
                                            @Override
                                            public void onItemClick(String paswd) {
                                                String flagValue = null;
                                                if ("0".equals(valuesBean.getValue())) {
                                                    flagValue = "1";
                                                } else if ("1".equals(valuesBean.getValue())) {
                                                    flagValue = "0";
                                                }
                                                if (flagValue == null) {
                                                    ToastUtil.showToast("未知远控指标状态，为确保安全，禁止更改");
                                                    return;
                                                }
                                                setPasWdToFacility(paswd, valuesBean.getFullCode(), flagValue);
                                            }
                                        });
                                        mControlDialog.show();
                                    }
                                } else {
                                    if (position == 0) {
                                        // 跳转到历史数据
                                        Intent intent = new Intent(StationRealDataActivity.this, ChartActivity.class);
                                        intent.putExtra("fullCode", valuesBean.getFullCode());
                                        startActivity(intent);
                                    } else if (position == 1) {
                                        //刷新echart表格 目前只有首创
                                        refreshCurve(valuesBean.getFullCode(), valuesBean.getName());
                                    }
                                }

                            }
                        });
                dialog.show();
            }
        });
    }

    /**
     * SPT修改指标状态
     *
     * @param fullCode
     * @param valueName item名称
     * @param dataMold
     * @param value
     */
    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    private void sptShowControlTargetDialog(final String fullCode, final String valueName, int dataMold, final String value) {
        Bundle bundle = new Bundle();
        String flagStr;
        flagStr = "是否确认开启" + valueName;
        switch (dataMold) {
            case 0://开关量
                mControlTargetDialog = HxSPTControlTargetDialog.newInstance();
                mControlTargetDialog.show(getFragmentManager(), "target");
                bundle.putString(Constants.TARGET_NAME, flagStr);
                mControlTargetDialog.setArguments(bundle);
                mControlTargetDialog.setOnItemEnsureClickListener(new HxSPTControlTargetDialog.OnItemEnsureClickListener() {
                    @Override
                    public void onItemEnsureClick() {
                        String flagValue = null;
                        if ("0".equals(value)) {
                            flagValue = "1";
                        } else if ("1".equals(value)) {
                            flagValue = "0";
                        }
                        setControlState(fullCode, flagValue);
                    }
                });
                break;
            case 1://模拟量
                amendSimulate(fullCode, valueName);
                break;
            default:
                break;

        }


    }

    /**
     * 修改模拟量
     */
    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    private void amendSimulate(final String fullCode, String valueName) {
        Bundle bundle = new Bundle();
        final HxSPTSimulateTargerDialog simulateTargerDialog = HxSPTSimulateTargerDialog.newInstance();
        simulateTargerDialog.show(getFragmentManager(), "simulate");
        bundle.putString(Constants.TARGET_NAME, valueName);
        simulateTargerDialog.setArguments(bundle);
        simulateTargerDialog.setOnItemSimulateClickListener(new HxSPTSimulateTargerDialog.OnItemSimulateClickListener() {
            @Override
            public void onItemSimulateClick(String value) {
                simulateTargerDialog.dismiss();
                setControlState(fullCode, value);
            }
        });
    }

    /**
     * 刷新曲线数据
     *
     * @param fullCode
     */
    private void refreshCurve(String fullCode, final String name) {
        Call<HxScStationRealDTO> minValue = HxTransmitRequestHelper.getRequestApi().getMaxMinValue(fullCode, System.currentTimeMillis());
        autoClose(minValue);
        minValue.enqueue(new Callback<HxScStationRealDTO>() {
            @Override
            public void onResponse(Call<HxScStationRealDTO> call, Response<HxScStationRealDTO> response) {
                HxScStationRealDTO realDTO = response.body();
                if (response.isSuccessful() && realDTO != null) {
                    List<Integer> maxStr = new ArrayList<>();
                    List<Integer> mixStr = new ArrayList<>();
                    String minStr = null;
                    for (int i = 0; i < realDTO.getData().size(); i++) {
                        maxStr.add(realDTO.getData().get(i).getMaxValue());
                        mixStr.add(realDTO.getData().get(i).getMinValue());
                    }
                    HxLogUtils.i("--max-->" + maxStr + "--mix-->" + maxStr);
                    HxLogUtils.i("--max-->" + maxStr.toString() + "--mix-->" + maxStr.toString());

                }
            }

            @Override
            public void onFailure(Call<HxScStationRealDTO> call, Throwable t) {

            }
        });
    }

    /**
     * 设置密码到设备中
     */
    private void setPasWdToFacility(String paswd, final String fullCode, final String value) {
        Call<BaseDTO> pass = HxTransmitRequestHelper.getRequestApi().getPass(paswd);
        autoClose(pass);
        pass.enqueue(new Callback<BaseDTO>() {
            @Override
            public void onResponse(Call<BaseDTO> call, Response<BaseDTO> response) {
                BaseDTO body = response.body();
                if (response.isSuccessful() && body != null) {
                    if (body.getState() == 1) {
                        setControlState(fullCode, value);
                    } else {
                        ToastUtil.showToast(body.getMsg());
                    }
                } else {
                    ToastUtil.showToast("验证异常");
                }

            }

            @Override
            public void onFailure(Call<BaseDTO> call, Throwable t) {
                ToastUtil.showToast(t.getMessage());
            }
        });
    }

    /**
     * 控制设备状态
     */
    private void setControlState(String fullCode, String value) {
        Call<HxChangeValveBean> changeValue = HxTransmitRequestHelper.getRequestApi().getChangeValue(fullCode, value);
        autoClose(changeValue);
        changeValue.enqueue(new Callback<HxChangeValveBean>() {
            @Override
            public void onResponse(Call<HxChangeValveBean> call, Response<HxChangeValveBean> response) {
                HxChangeValveBean body = response.body();
                if (body.isSuccessful()) {
                    ToastUtil.showToast("修改成功");
                } else {
                    ToastUtil.showToast("修改失败");
                }
            }

            @Override
            public void onFailure(Call<HxChangeValveBean> call, Throwable t) {
                ToastUtil.showToast(t.getMessage());
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    @OnClick({R.id.tv_device_box, R.id.more})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_device_box:
                //底部弹窗
                if (mDialog == null) {
                    mDialog = new BottomSheetDialog(this, deviceListAdapter, new BottomSheetDialog.OnItemClickListener() {
                        @Override
                        public void onItemClick(int position) {
                            //传adapter进去 设置item点击事件无效
                        }
                    });

                    setOnDialogItemClickListener();
                }
                mDialog.show();
                mDialog.setTitle("选择设备").setTitleVisibility(true).setTitleColor(R.color.text_color_3).setCancleColor(R.color.text_color_3);
                break;
            case R.id.more://初始化
                //底部弹框 初始化 刷新等操作   设备默认状态
                sptRemoteContralDialog = new SptRemoteContralDialog();
                //在弹出底部弹框之前 获取设备状态
                if (deviceStata == -4) {
                    getTargetStatus(true);
                } else {
                    sptRemoteContralDialog.show(getFragmentManager(), "1");
                    Bundle args = new Bundle();
                    args.putInt("status", deviceStata);
                    sptRemoteContralDialog.setArguments(args);
                }
                sptRemoteContralDialog.setOnclikLiestner(new SptRemoteContralDialog.ClickLiestner() {
                    @Override
                    public void onClick(int index) {
                        // 0刷新 1初始化 2手动 3自动
                        switch (index) {
                            case 0:
                                //progress dialog
                                getTargetStatus(true);
                                break;
                            case 1:
                                //初始化
                                HxSPTInitTargetFragment mTargetFragment = HxSPTInitTargetFragment.newInstance();
                                mTargetFragment.show(getFragmentManager(), "init");
                                mTargetFragment.setInitLisetener(new HxSPTInitTargetFragment.onClickedInitLisetener() {
                                    @Override
                                    public void init() {
                                        initTarget();
                                    }
                                });
                                break;
                            case 2:
                                autoMode(0);
                                break;
                            case 3:
                                autoMode(1);
                                break;
                        }

                    }
                });
                break;
            default:
                break;
        }
    }

    /**
     * 手动自动控制 0 手动 1自动
     */
    private void autoMode(int goalMode) {
        Call<BaseDTO> call = HxTransmitRequestHelper.getRequestApi().autoMode(mStationCode, goalMode);
        final ProgressDialog waitDialog = DialogUtil.getWaitDialog(this, "请稍等...");
        waitDialog.show();
        autoClose(call);
        call.enqueue(new Callback<BaseDTO>() {
            @Override
            public void onResponse(Call<BaseDTO> call, Response<BaseDTO> response) {
                waitDialog.dismiss();
                BaseDTO body = response.body();
                if (body != null && body.isSuccessful()) {
                    ToastUtil.showToast(body.getMsg());
                } else {
                    ToastUtil.showToast("设备远控失败");
                }
            }

            @Override
            public void onFailure(Call<BaseDTO> call, Throwable t) {
                waitDialog.dismiss();
                ToastUtil.showToast("设备远控失败");
            }
        });
    }

    /**
     * 刷新设备状态
     *
     * @param isDaiog
     */
    private void getTargetStatus(final boolean isDaiog) {
        Call<HxSPTTargetDTO> call = HxTransmitRequestHelper.getRequestApi().getTargetStatus(mStationCode);
        final ProgressDialog waitDialog = DialogUtil.getWaitDialog(this, "请稍等...");
        if (isDaiog) {
            waitDialog.show();
        }
        autoClose(call);
        call.enqueue(new Callback<HxSPTTargetDTO>() {
            @Override
            public void onResponse(Call<HxSPTTargetDTO> call, Response<HxSPTTargetDTO> response) {
                waitDialog.dismiss();
                if (response.body() != null && response.isSuccessful()) {
                    HxSPTTargetDTO.DataBean dataBean = response.body().getData();
                    if (dataBean != null) {
                        deviceStata = dataBean.getStatus();
                        Log.e("lwjing", "deviceStata=" + dataBean.getStatus());
                        if (isDaiog) {
                            //更新设备状态
                            Log.e("lwjing", "isResumed=" + sptRemoteContralDialog.isResumed());
                            if (!sptRemoteContralDialog.isResumed()) {
                                sptRemoteContralDialog.show(getFragmentManager(), "init");
                                //bandle Fragment already active 再用args刷新则会报错
                                Bundle args = new Bundle();
                                args.putInt("status", dataBean.getStatus());
                                sptRemoteContralDialog.setArguments(args);
                            }else{
                                sptRemoteContralDialog.refreshStata(dataBean.getStatus());
                            }

                        }
                    } else {
                        //接口请求获取不到数据的情况下  默认设备不在线
                        deviceStata = -1;
                    }
                } else {
                    deviceStata = -1;
                }
            }

            @Override
            public void onFailure(Call<HxSPTTargetDTO> call, Throwable t) {
                waitDialog.dismiss();
                deviceStata = -1;
            }
        });
    }

    /**
     * 初始化指标
     */
    private void initTarget() {
        Call<HxSPTTargetDTO> call = HxTransmitRequestHelper.getRequestApi().getSPTInitialized(mStationCode);
        final ProgressDialog waitDialog = DialogUtil.getWaitDialog(this, "请稍等....");
        waitDialog.show();
        autoClose(call);
        call.enqueue(new Callback<HxSPTTargetDTO>() {
            @Override
            public void onResponse(Call<HxSPTTargetDTO> call, Response<HxSPTTargetDTO> response) {
                waitDialog.dismiss();

                if (response.isSuccessful() && response.body() != null) {
                    HxSPTTargetDTO.DataBean dataBean = response.body().getData();
                    if (dataBean == null) {
                        ToastUtil.showToast("初始化失败");
                        return;
                    }
                    isInitTarget = true;
                    switch (dataBean.getStatus()) {
                        case -1://不在线
                            ToastUtil.showToast("设备不在线");
                            break;
                        case 0://手动
                            ToastUtil.showToast("初始化成功");
                            break;
                        case 1://自动
                            ToastUtil.showToast("初始化成功");
                            break;
                        default:
                            break;

                    }
                } else {
                    ToastUtil.showToast("初始化失败");
                }
            }

            @Override
            public void onFailure(Call<HxSPTTargetDTO> call, Throwable t) {
                ToastUtil.showToast("初始化失败");
                waitDialog.dismiss();
            }
        });
    }

    /**
     * 设备分栏条目点击事件
     */
    private void setOnDialogItemClickListener() {
        deviceListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                //通过设备号获取相应的设备的指标
                // ToastUtil.showToast(deviceList.get(position).getDeviceName());
                deviceCode = deviceList.get(position).getDeviceCode();
                ApiRequestData(LOAD_SUCCESS, isTimer, null);
                mDialog.dismiss();
                mTvDeviceBox.setText(deviceList.get(position).getDeviceName());
            }
        });
    }


    class DeviceListAdapter extends BaseQuickAdapter<DeviceListDTO.DeviceDateBean, BaseViewHolder> {

        public DeviceListAdapter() {
            super(R.layout.item_select);
        }

        @Override
        protected void convert(BaseViewHolder viewHolder, DeviceListDTO.DeviceDateBean deviceDateBean) {
            TextView item = viewHolder.getView(R.id.item);
            loadText(item, deviceDateBean.getDeviceName());
        }
    }

    /**
     * 释放资源
     */
    private void releaseTimer() {
        if (mMTimer != null && mTimerTask != null) {
            if (mDate != null) {
                mDate.setText("");
            }
            if (mMTimer != null) {
                mMTimer.cancel();
            }
        }
    }
}